package Year21_Month11.day1105;

public class Soultion2 {
    public String tree2str(TreeNode root) {
        StringBuilder res = new StringBuilder();
        doTree2str(root,res);
        return res.toString();
    }
    public void doTree2str(TreeNode root,StringBuilder res){
        if(root == null){
            return;
        }
        res.append(root.val);
        if(root.left == null && root.right == null){
            return;
        }

        res.append("(");
        doTree2str(root.left,res);
        res.append(")");
        if(root.right != null){
            res.append("(");
            doTree2str(root.right,res);
            res.append(")");
        }
    }
}
